package org.akomantoso.core.data;

import java.io.IOException;
import java.util.Calendar;

import org.akomantoso.core.data.attribute.Date;
import org.akomantoso.core.data.attribute.Name;
import org.akomantoso.core.data.attribute.Refers;
import org.akomantoso.core.data.attribute.Show;
import org.akomantoso.core.data.attribute.Number;
import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;
import org.akomantoso.core.io.util.DateUtil;
import org.akomantoso.core.visitor.AKVisitable;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * The element publication is the metadata container specifying a publication event for the FRBR expression of the
 * document.
 * 
 * <pre>
 * 	<xsd:element name="publication">
 * 		<xsd:complexType>
 * 			<xsd:complexContent>
 * 				<xsd:extension base="metaopt">
 * 					<xsd:attributeGroup ref="date"/>
 * 					<xsd:attributeGroup ref="show"/>
 * 					<xsd:attributeGroup ref="name"/>
 * 					<xsd:attributeGroup ref="number"/>
 * 					<xsd:attributeGroup ref="refers"/>
 * 				</xsd:extension>
 * 			</xsd:complexContent>
 * 		</xsd:complexType>
 * 	</xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 * 
 */
public final class Publication extends MetaOpt implements Date, Show, Name, Number, Refers, Externalizable, AKVisitable {

	public static final String ELEMENT = "publication";
	
	// Mandatory
	private Calendar _date;
	// Mandatory
	private String _showAs;
	// Optional
	private String _shortForm;
		
	private String _name;
	private String _number;
	private String _refersTo;

	/**
	 * @see org.akomantoso.core.data.attribute.Refers#getRefersTo()
	 */
	public String getRefersTo() {
		return _refersTo;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Refers#setRefersTo(java.lang.String)
	 */
	public void setRefersTo(String refersTo) {
		_refersTo = refersTo;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Number#getNumber()
	 */
	public String getNumber() {
		return _number;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Number#setNumber(java.lang.Integer)
	 */
	public void setNumber(String number) {
		_number = number;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Name#getName()
	 */
	public String getName() {
		return _name;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Name#setName(java.lang.String)
	 */
	public void setName(String name) {
		_name = name;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Show#getShowAs()
	 */
	public String getShowAs() {
		return _showAs;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Show#setShowAs(java.lang.String)
	 */
	public void setShowAs(String showAs) {
		_showAs = showAs;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Show#getShortForm()
	 */
	public String getShortForm() {
		return _shortForm;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Show#setShortForm(java.lang.String)
	 */
	public void setShortForm(String shortForm) {
		_shortForm = shortForm;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Date#getDate()
	 */
	public Calendar getDate() {
		return _date;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Date#setDate(java.util.Calendar)
	 */
	public void setDate(Calendar date) {
		_date = date;
	}

	
	/**
	 * @see org.akomantoso.core.data.MetaOpt#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeDate(out, this);
		OutputUtil.writeName(out, this);
		OutputUtil.writeRefers(out, this);
		OutputUtil.writeShow(out, this);
		OutputUtil.writeNumber(out, this);
		super.writeExternal(out);
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.data.MetaOpt#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		
		// Mandatory
		_date = DateUtil.convert(in.readAttribute(Date.ATTRIBUTE));
		_showAs = in.readAttribute(Show.ATTRIBUTE_SHOW_AS);
		_name = in.readAttribute(Name.ATTRIBUTE);
		
		// Optional
		_number =  in.readAttribute(Number.ATTRIBUTE);
		_shortForm = in.readAttribute(Show.ATTRIBUTE_SHORT_FORM);
		_refersTo = in.readAttribute(Refers.ATTRIBUTE);
		
		super.readExternal(in);
	}

	/**
	 * @see org.akomantoso.core.visitor.AKVisitable#accept(org.akomantoso.core.visitor.AKVisitor)
	 */
	@Override
	public void accept(AKVisitor visitor) {
		visitor.visit(this);
	}

}